//
//  LZStatusRetweetView.swift
//  Weibo
//
//  Created by lzing on 16/3/27.
//  Copyright © 2016年 lzing. All rights reserved.
//

import UIKit
import SnapKit

class LZStatusRetweetView: UIView {
    
    var retweetViewBottomConstraint : Constraint?
    
    // 被转发的微博模型
    var retweetStatus : LZStatus? {
        didSet {
            guard let screen_name = retweetStatus?.user?.screen_name else {
                // 没有转发名称
                return
            }
            guard let attributeText = retweetStatus?.attributeText else {
                // 没有转发内容
                return
            }
            let nameAttribute = NSMutableAttributedString(string: "@\(screen_name): ")
            nameAttribute.appendAttributedString(attributeText)
            // 转发内容
            retweetContentLabel.attributedText = nameAttribute
            // 卸载约束
            retweetViewBottomConstraint?.uninstall()
            // 判断是否有图片
            if let pictureURLs = retweetStatus?.pictureURLs {
                // 显示图片
                retweetPictureView.hidden = false
                // 设置图片数据
                retweetPictureView.pictureURLs = pictureURLs
                // 添加约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(LZStatusCellMargin).constraint
                })
            } else {
                // 隐藏图片
                retweetPictureView.hidden = true
                // 添加约束
                self.snp_makeConstraints(closure: { (make) -> Void in
                    self.retweetViewBottomConstraint = make.bottom.equalTo(retweetContentLabel).offset(LZStatusCellMargin).constraint
                })
            }
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        // 添加子控件
        addSubview()
        // 添加约束
        addConstraint()
    }
    
    // 添加子控件
    private func addSubview() {
        addSubview(backgroundButton)
        addSubview(retweetContentLabel)
        addSubview(retweetPictureView)
    }
    
    // 添加约束
    private func addConstraint() {
        // 背景按钮
        backgroundButton.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self)
        }
        
        // 转发内容
        retweetContentLabel.snp_makeConstraints { (make) -> Void in
            make.top.left.equalTo(self).offset(LZStatusCellMargin)
            make.right.equalTo(self).offset(-LZStatusCellMargin)
        }
        
        // 转发微博配图
        retweetPictureView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(retweetContentLabel)
            make.top.equalTo(retweetContentLabel.snp_bottom).offset(LZStatusCellMargin)
//            make.size.equalTo(CGSize(width: 200, height: 200))
        }
        
        self.snp_makeConstraints { (make) -> Void in
           self.retweetViewBottomConstraint = make.bottom.equalTo(retweetPictureView).offset(LZStatusCellMargin).constraint
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - lazy
    // 背景按钮
    private lazy var backgroundButton : UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "retweet_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "retweet_background_highlighted"), forState: UIControlState.Highlighted)
        return button
    }()
    
    // 被转发微博内容
    private lazy var retweetContentLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(LZStatusCellContentSize)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        // 设置label宽度
        label.preferredMaxLayoutWidth = UIScreen.width - 2 * LZStatusCellMargin
        return label
    }()
    
    // 微博配图
    private lazy var retweetPictureView : LZStatusPictureView = LZStatusPictureView()
}
